package org.sunnyleaf.votedemo.server;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.sunnyleaf.votedemo.client.data.Vote;
import org.sunnyleaf.votedemo.client.data.VoteItem;
import org.sunnyleaf.votedemo.client.data.VoteRecord;
import org.sunnyleaf.votedemo.client.data.VoteRecordIndex;

import com.google.appengine.api.datastore.Key;

public class DataControler {
	
	static public VoteRecord getVoteRecrod( Key key ){
/*		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery( VoteRecord.class );
		query.setFilter( "Key == " + key.toString() );
		List<VoteRecord> results = (List<VoteRecord>)query.execute();
		return results.get(0);*/
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		VoteRecord res = pm.getObjectById( VoteRecord.class, key );
		return res;
	}

	static public void deleteObjByID( Class<?> c, Object id ){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try{
			Object res = pm.getObjectById( c , id);		
			pm.deletePersistent(res);
		}finally{
			pm.close();
		}
	}
	
	static public void saveObj( Object obj ){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try{
			pm.makePersistent( obj );
		}
		finally{
			pm.close();
		}
	}
	
	static public Vote getVoteByID( Long id ){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Vote res = pm.getObjectById( Vote.class, id );
		return res;
	}
	
	static public VoteRecord getVoteRecord( String sha1 ){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery( "select from VoteRecord where sha1 == "+sha1 );
		return (VoteRecord)query.execute();
	}
	
	@SuppressWarnings("unchecked")
	static public List<VoteRecord>getVoteRecrods( Long voteID ){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery( VoteRecord.class );
		query.setFilter( "voteId == " + voteID.toString() );
		List<VoteRecord> res = (List<VoteRecord>)query.execute();
		List<VoteRecord> returnRes = new ArrayList<VoteRecord>();
		for (VoteRecord v : res ) {
			returnRes.add(v);
		}
		return returnRes;
	}
	
	@SuppressWarnings("unchecked")
	static public List<VoteRecord> getAllVoteRecords(){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery( "select from VoteRecord" );
		List<VoteRecord> res = (List<VoteRecord>)query.execute();
		List<VoteRecord> returnRes = new ArrayList<VoteRecord>();
		for (VoteRecord v : res ) {
			returnRes.add(v);
		}
		return returnRes;
	}
	
	@SuppressWarnings("unchecked")
	static public List<VoteItem> getAllVoteItems(Long voteId){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery( VoteItem.class );
		query.setFilter( "voteID == voteid" );
		query.declareParameters("Long voteid");
		List<VoteItem> res = (List<VoteItem>)query.execute( voteId );
		
		List<VoteItem> returnRes = new ArrayList<VoteItem>();
		
		for (VoteItem v : res ) {
			ArrayList<String> itemarray = (ArrayList<String>)v.getItemArray();
			ArrayList<Integer> countsarray = (ArrayList<Integer>)v.getCountsArray();
			v.setItemArray( new ArrayList<String>());
			v.setCountsArray(new ArrayList<Integer>());
			for( String s:itemarray ){
				v.addItem(s);
			}
			
			int index = 0;
			for( Integer c:countsarray ){
				v.setItemCount(index++, c );
			}
	
			returnRes.add(v);
		}
		return returnRes;
	}
	
	@SuppressWarnings("unchecked")
	static public List<Vote> getAllVote(){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery( Vote.class );
		List<Vote> res = (List<Vote>)query.execute();
		List<Vote> returnRes = new ArrayList<Vote>();
		for (Vote v : res ) {
			returnRes.add(v);
		}
		return returnRes;
	}
	
	@SuppressWarnings("unchecked")
	static public List<Vote> getVotesOwnTo( String owner ){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery(Vote.class, "owner==ownerEmail");
		q.declareParameters("String ownerEmail");
		List<Vote> t1 = (List<Vote>) q.execute( owner );
		List<Vote> pvs2 = new ArrayList<Vote>();

		for (Vote v : t1) {
			pvs2.add(v);
		}
		return pvs2;
	}
	
	@SuppressWarnings("unchecked")
	static public List<Vote> getVotesAttendedBy( String user ){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery( VoteRecord.class, "account == email" );
		q.declareParameters("String email");
		List<VoteRecord> records = (List<VoteRecord>)q.execute(user);
		List<Vote> votes = new ArrayList<Vote>();
		for( VoteRecord record:records ){
			votes.add(getVoteByID(record.getVoteId()));
		}
		return votes;
	}
	
	@SuppressWarnings("unchecked")
	static public List<VoteRecordIndex> getVoteRecordIndexes( Long voteId ){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery( VoteRecordIndex.class, "voteID == "+voteId.toString() );
		List<VoteRecordIndex> res = ( List<VoteRecordIndex> )q.execute();
		List<VoteRecordIndex> returnRes = new ArrayList<VoteRecordIndex>();
		for( VoteRecordIndex vr:res ){
			returnRes.add(vr);
		}	
		return returnRes;
	}
	
	static public Object getObjectById( Class<?> c, Object id ){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Object obj = null;
		try{
			obj = pm.getObjectById(c, id );
		}finally{
			pm.close();
		}
		return obj;
	}
	
}
